<template>
  <div class="forum-manager-container">
    <div class="manager-header">
      <div class="header-title">
        <h2>
          <el-icon class="header-icon"><Document /></el-icon>
          我的帖子管理
        </h2>
      </div>
      <div class="header-stats">
        <div class="stat-card">
          <el-icon class="stat-icon"><ChatDotRound /></el-icon>
          <div class="stat-info">
            <div class="stat-number">{{ total }}</div>
            <div class="stat-label">总帖子数</div>
          </div>
        </div>
        <el-button type="primary" class="refresh-btn" @click="getPagedPosts" :loading="loading">
          <el-icon class="btn-icon"><Refresh /></el-icon>
          刷新数据
        </el-button>
      </div>
    </div>

    <div class="table-container" style="padding:0; border-radius:0; border:none;">
      <el-table :data="myPosts" style="width: 100%" v-loading="loading" class="modern-table" border :height="400">
        <el-table-column prop="title" label="标题" align="center" width="150">
          <template #default="scope">
            <div class="title-cell" style="align-items:center;">
              <el-icon class="title-icon"><Star /></el-icon>
              <span class="title-text">{{ scope.row.title }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="分类" align="center" width="110">
          <template #default="scope">
            <el-tag class="category-tag" type="info">
              <el-icon class="tag-icon"><Collection /></el-icon>
              {{ getCategoryName(scope.row.categoryId) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="likeCount" label="点赞数" align="center" width="90">
          <template #default="scope">
            <div class="count-cell" style="align-items:center;">
              <el-icon class="count-icon like-icon"><StarFilled /></el-icon>
              <span>{{ scope.row.likeCount }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="commentCount" label="评论数" align="center" width="90">
          <template #default="scope">
            <div class="count-cell" style="align-items:center;">
              <el-icon class="count-icon comment-icon"><ChatRound /></el-icon>
              <span>{{ scope.row.commentCount }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" align="center" width="150">
          <template #default="scope">
            <div class="time-cell" style="align-items:center;">
              <el-icon class="time-icon"><Clock /></el-icon>
              <span>{{ formatDate(scope.row.createTime) }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" label="更新时间" align="center">
          <template #default="scope">
            <div class="time-cell" style="align-items:center;">
              <el-icon class="time-icon"><EditPen /></el-icon>
              <span>{{ formatDate(scope.row.updateTime) }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="当前状态" align="center" width="110">
          <template #default="scope">
            <el-tag 
              :type="scope.row.status === 0 ? 'success' : (scope.row.status === 1 ? 'danger' : 'warning')"
              class="status-tag"
            >
              <el-icon class="status-icon">
                <Check v-if="scope.row.status === 0" />
                <Close v-else-if="scope.row.status === 1" />
                <Hide v-else />
              </el-icon>
              {{ statusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="230">
          <template #default="scope">
            <div class="action-buttons">
              <el-button size="small" class="edit-btn" @click="editPost(scope.row)">
                <el-icon class="btn-icon"><Edit /></el-icon>
                编辑
              </el-button>
              <el-button size="small" class="delete-btn" @click="deletePost(scope.row)">
                <el-icon class="btn-icon"><Delete /></el-icon>
                删除
              </el-button>
              <el-button size="small" class="hide-btn" @click="hidePost(scope.row)">
                <el-icon class="btn-icon">
                  <View v-if="scope.row.status === 2" />
                  <Hide v-else />
                </el-icon>
                {{ scope.row.status === 2 ? '显示' : '隐藏' }}
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      
      <el-pagination
        v-model:current-page="page"
        :page-size="pageSize"
        :total="total"
        layout="prev, pager, next"
        @current-change="getPagedPosts"
        class="custom-pagination"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, watch } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getPostsByAuthorId, deletePost as apiDeletePost } from '../../api/post'
import { getMyAllPosts, updatePost } from '../../api/forum'
import { useUserStore } from '../../stores/user'
import { useRouter } from 'vue-router'
import { useCategoryStore } from '../../stores/category'
import { 
  Document, ChatDotRound, Refresh, Star, Collection, StarFilled, 
  ChatRound, Clock, EditPen, Check, Close, Hide, Edit, Delete, View 
} from '@element-plus/icons-vue'

const userStore = useUserStore()
const categoryStore = useCategoryStore()
const router = useRouter()

const loading = ref(false)
const page = ref(1)
const pageSize = 6
const total = ref(0)

const allPosts = ref<any[]>([])
const myPosts = ref<any[]>([])

// 加载所有帖子并本地分页
async function getPagedPosts() {
  loading.value = true
  try {
    const authorId = userStore.id || 1 // 实际应取当前登录用户id
    const res = await getMyAllPosts(Number(authorId))
    // 兼容后端返回结构
    const list = Array.isArray(res.data) ? res.data : []
    allPosts.value = list
    total.value = list.length
    // 本地分页
    const start = (page.value - 1) * pageSize
    myPosts.value = list.slice(start, start + pageSize)
  } finally {
    loading.value = false
  }
}

watch(page, getPagedPosts)
onMounted(getPagedPosts)

//状态显示标签
function statusText(status: number) {
  switch (status) {
    case 0: return '正常'
    case 1: return '删除'
    case 2: return '隐藏'
    default: return '未知'
  }
}

function editPost(row: any) {
  window.open(`/forum-edit/${row.id}`, '_blank')
}

async function deletePost(row: any) {
  await ElMessageBox.confirm('确定要删除该帖子吗？', '提示', { type: 'warning' })
  await apiDeletePost(row.id)
  ElMessage.success('删除成功')
  getPagedPosts()
}

async function hidePost(row: any) {
  const newStatus = row.status === 2 ? 0 : 2
  await updatePost({ id: row.id, status: newStatus })
  ElMessage.success(newStatus === 2 ? '已隐藏' : '已取消隐藏')
  getPagedPosts() // 状态变化后刷新数据，确保更新时间同步
}

function handleCurrentChange(newPage: number) {
  page.value = newPage
}

function formatDate(dateStr: string) {
  if (!dateStr) return ''
  const d = new Date(dateStr)
  const year = d.getFullYear()
  const month = String(d.getMonth() + 1).padStart(2, '0')
  const day = String(d.getDate()).padStart(2, '0')
  return `${year}-${month}-${day}`
}

function getCategoryName(categoryId: number) {
  const cat = categoryStore.categories.find((c: any) => c.id === categoryId)
  return cat ? cat.name : '未知分类'
}
</script>

<style scoped>
.forum-manager-layout {
  display: flex;
  min-height: 80vh;
  background: transparent;
  width: 70%;
}
.forum-manager-main {
  flex: 1;
  margin: 40px 40px 40px 0;
}

/* 主容器样式 */
.forum-manager-container {
  max-width: 1200px;
  margin: 0 auto;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.98) 0%, rgba(252, 250, 255, 0.98) 100%);
  border-radius: var(--radius-xl);
  box-shadow: 0 8px 40px rgba(162, 89, 236, 0.15), 0 4px 16px rgba(162, 89, 236, 0.1);
  padding: 30px 40px;
  backdrop-filter: blur(20px);
  border: 1px solid rgba(162, 89, 236, 0.1);
}

/* 头部样式 */
.manager-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 10px;
  padding-bottom: 5px;
  border-bottom: 2px solid rgba(162, 89, 236, 0.1);
}

.header-title {
  flex: 1;
}

.header-title h2 {
  font-size: 2.2rem;
  font-weight: 700;
  background: linear-gradient(135deg, #a259ec 0%, #5b6ee1 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin: 8px 0;
  display: flex;
  align-items: center;
  gap: 12px;
}

.header-icon {
  font-size: 2.2rem;
  color: var(--primary-purple, #a259ec);
  /* 保持和下方统计卡片图标风格一致 */
  background: rgba(255, 255, 255, 0.8);
  padding: 12px;
  border-radius: 50%;
  margin-right: 8px;
}

.header-stats {
  display: flex;
  align-items: center;
  gap: 24px;
}

.stat-card {
  background: linear-gradient(135deg, rgba(162, 89, 236, 0.1) 0%, rgba(91, 110, 225, 0.1) 100%);
  padding: 20px;
  border-radius: var(--radius-lg, 12px);
  border: 1px solid rgba(162, 89, 236, 0.15);
  display: flex;
  align-items: center;
  gap: 16px;
  transition: all 0.3s ease;
  min-width: 140px;
}

.stat-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 32px rgba(162, 89, 236, 0.2);
}

.stat-icon {
  font-size: 32px;
  color: var(--primary-purple, #a259ec);
  background: rgba(255, 255, 255, 0.8);
  padding: 12px;
  border-radius: 50%;
}

.stat-info {
  text-align: left;
}

.stat-number {
  font-size: 24px;
  font-weight: 700;
  color: var(--primary-purple, #a259ec);
  line-height: 1;
}

.stat-label {
  font-size: 14px;
  color: var(--text-muted, #6b7280);
  margin-top: 4px;
}

.refresh-btn {
  background: linear-gradient(135deg, #a259ec 0%, #8b5cf6 100%);
  color: white;
  border: none;
  border-radius: var(--radius-md, 8px);
  padding: 12px 24px;
  font-weight: 600;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 8px;
}

.refresh-btn:hover {
  background: linear-gradient(135deg, #9333ea 0%, #7c3aed 100%);
  color: white;
  transform: translateY(-2px);
  box-shadow: 0 8px 32px rgba(162, 89, 236, 0.3);
}

/* 表格容器 */
.table-container {
  margin-bottom: 32px;
  /* border-radius: var(--radius-lg, 12px); */
  overflow: hidden;
  /* border: 1px solid rgba(162, 89, 236, 0.1); */
  width: 100%;
  margin-left: auto;
  margin-right: auto;
  padding: 0;
}

.modern-table {
  border-radius: var(--radius-lg, 12px);
  background: rgba(255, 255, 255, 0.95);
  overflow: hidden;
}

.modern-table :deep(.el-table__header th),
.modern-table :deep(.el-table__body td) {
  padding: 12px 8px !important;
  box-sizing: border-box;
  text-align: center !important;
  vertical-align: middle;
}

.modern-table :deep(.el-table__header) {
  background: linear-gradient(135deg, rgba(162, 89, 236, 0.1) 0%, rgba(91, 110, 225, 0.1) 100%);
}

.modern-table :deep(.el-table__header th) {
  background: transparent;
  color: var(--primary-purple, #a259ec);
  font-weight: 600;
  border-bottom: 2px solid rgba(162, 89, 236, 0.2);
  padding: 16px 12px;
}

.modern-table :deep(.el-table__header th .cell) {
  text-align: center !important;
  justify-content: center;
  display: flex;
}

.modern-table :deep(.el-table__body tr) {
  transition: all 0.3s ease;
}

.modern-table :deep(.el-table__body tr:hover) {
  background: rgba(162, 89, 236, 0.05);
}

.modern-table :deep(.el-table__body td) {
  padding: 16px 12px;
  border-bottom: 1px solid rgba(162, 89, 236, 0.1);
}

/* 表格单元格样式 */
.title-cell {
  display: flex;
  align-items: center;
  gap: 8px;
}

.title-icon {
  color: #ffd700;
  font-size: 16px;
}

.title-text {
  font-weight: 500;
  color: var(--text-primary, #1f2937);
}

.category-tag {
  display: flex;
  align-items: center;
  gap: 6px;
  border-radius: var(--radius-sm, 6px);
  font-weight: 500;
}

.tag-icon {
  font-size: 14px;
}

.count-cell {
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 600;
}

.count-icon {
  font-size: 16px;
}

.like-icon {
  color: #ef4444;
}

.comment-icon {
  color: #3b82f6;
}

.time-cell {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  color: var(--text-secondary, #6b7280);
}

.time-icon {
  font-size: 14px;
  color: var(--primary-purple, #a259ec);
}

.status-tag {
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 500;
  border-radius: var(--radius-sm, 6px);
}

.status-icon {
  font-size: 14px;
}

/* 操作按钮 */
.action-buttons {
  display: flex;
  gap: 1px; /* 原为8px，减小按钮间距 */
  align-items: center;
  justify-content: center;
}

.edit-btn {
  background: linear-gradient(135deg, #06b6d4 0%, #0891b2 100%);
  color: white;
  border: none;
  border-radius: var(--radius-sm, 6px);
  font-weight: 500;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 4px;
}

.edit-btn:hover {
  background: linear-gradient(135deg, #0891b2 0%, #0e7490 100%);
  color: white;
  transform: translateY(-1px);
  box-shadow: 0 4px 16px rgba(6, 182, 212, 0.3);
}

.delete-btn {
  background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
  color: white;
  border: none;
  border-radius: var(--radius-sm, 6px);
  font-weight: 500;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 4px;
}

.delete-btn:hover {
  background: linear-gradient(135deg, #dc2626 0%, #b91c1c 100%);
  color: white;
  transform: translateY(-1px);
  box-shadow: 0 4px 16px rgba(239, 68, 68, 0.3);
}

.hide-btn {
  background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%);
  color: white;
  border: none;
  border-radius: var(--radius-sm, 6px);
  font-weight: 500;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 4px;
}

.hide-btn:hover {
  background: linear-gradient(135deg, #d97706 0%, #b45309 100%);
  color: white;
  transform: translateY(-1px);
  box-shadow: 0 4px 16px rgba(245, 158, 11, 0.3);
}

.btn-icon {
  font-size: 14px;
}

/* 分页样式 */
.custom-pagination {
  margin-top: 12px;
  display: flex;
  justify-content: center;
}

.custom-pagination :deep(.el-pager li) {
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid rgba(162, 89, 236, 0.2);
  border-radius: var(--radius-md, 8px);
  margin: 0 4px;
  transition: all 0.3s ease;
  color: var(--text-secondary, #6b7280);
}

.custom-pagination :deep(.el-pager li:hover),
.custom-pagination :deep(.el-pager li.is-active) {
  background: linear-gradient(135deg, #a259ec 0%, #8b5cf6 100%);
  color: white;
  border-color: transparent;
  transform: translateY(-1px);
  box-shadow: 0 4px 16px rgba(162, 89, 236, 0.3);
}

.custom-pagination :deep(.btn-prev),
.custom-pagination :deep(.btn-next) {
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid rgba(162, 89, 236, 0.2);
  border-radius: var(--radius-md, 8px);
  color: var(--text-secondary, #6b7280);
  transition: all 0.3s ease;
}

.custom-pagination :deep(.btn-prev:hover),
.custom-pagination :deep(.btn-next:hover) {
  background: linear-gradient(135deg, #a259ec 0%, #8b5cf6 100%);
  color: white;
  border-color: transparent;
  transform: translateY(-1px);
  box-shadow: 0 4px 16px rgba(162, 89, 236, 0.3);
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .forum-manager-container {
    padding: 24px;
  }
  
  .manager-header {
    flex-direction: column;
    gap: 20px;
    align-items: stretch;
  }
  
  .header-stats {
    justify-content: space-between;
  }
  
  .action-buttons {
    flex-direction: column;
    gap: 4px;
  }
}

@media (max-width: 768px) {
  .forum-manager-container {
    padding: 16px;
  }
  
  .header-title h2 {
    font-size: 1.8rem;
  }
  
  .stat-card {
    min-width: auto;
    padding: 16px;
  }
  
  .modern-table :deep(.el-table__body td),
  .modern-table :deep(.el-table__header th) {
    padding: 12px 8px;
  }
}
</style>
